package cn.xzqwjw.taskmanager.service.impl;

import cn.xzqwjw.taskmanager.common.customEnum.ResponseCodeEnum;
import cn.xzqwjw.taskmanager.common.exception.CustomException;
import cn.xzqwjw.taskmanager.domain.pojo.Task;
import cn.xzqwjw.taskmanager.domain.vo.ResponseVo;
import cn.xzqwjw.taskmanager.repository.TaskMapper;
import cn.xzqwjw.taskmanager.service.ArticleService;
import cn.xzqwjw.taskmanager.utils.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.xzqwjw.taskmanager.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wjw
 */
@EnableTransactionManagement
@Service("TaskService")
@Slf4j
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

  private final ArticleService articleService;
  private final TaskMapper taskMapper;

  @Autowired
  public TaskServiceImpl(ArticleService articleService,
                         TaskMapper taskMapper) {
    this.articleService = articleService;
    this.taskMapper = taskMapper;
  }

  @Override
  public String add(Task task) {
    // 先添加任务对应的文章
    if (Objects.isNull(task.getArticle())) {
      throw new CustomException(ResponseCodeEnum.BAD_REQUEST);
    }
    int idArticle = articleService.add4Task(task.getArticle());
    if (idArticle == 0) {
      throw new CustomException(ResponseCodeEnum.ERROR_INSERT);
    }
    task.setIdArticle(idArticle);
    // 添加任务记录
    if (save(task)) {
      return JsonUtil.obj2Json(ResponseVo.success(ResponseCodeEnum.OK_CREATED));
    } else {
      throw new CustomException(ResponseCodeEnum.ERROR_INSERT);
    }
  }

  /**
   * 删除
   */
  @Override
  public String delById(int idTask) {
    if (removeById(idTask)) {
      return JsonUtil.obj2Json(ResponseVo.success(ResponseCodeEnum.OK_DELETED));
    } else {
      throw new CustomException(ResponseCodeEnum.ERROR_DELETE);
    }
  }

  @Override
  public String modifyAll(Task task) {
    // 先修改任务对应文章的相关内容
    if (!articleService.modifyAll4Task(task.getArticle())) {
      throw new CustomException(ResponseCodeEnum.ERROR_UPDATE);
    }
    // 修改任务
    if (updateById(task)) {
      return JsonUtil.obj2Json(ResponseVo.success(ResponseCodeEnum.OK_UPDATED));
    } else {
      throw new CustomException(ResponseCodeEnum.ERROR_UPDATE);
    }
  }

  /**
   * 设置任务状态
   * type = "enabled" 修改启用状态
   * type = "finished" 修改完成状态
   * type = "status" 修改审核状态
   */
  @Override
  public String patchStatus(int idTask, String type, String status) {
    if ("enabled".equals(type)) {
      taskMapper.patchEnabled(idTask);
    } else if ("finished".equals(type)) {
      taskMapper.patchFinished(idTask);
    } else if ("status".equals(type)) {
      Map<String, Object> paramMap = new HashMap<>();
      paramMap.put("id", idTask);
      paramMap.put("status", status);
      taskMapper.patchStatus(paramMap);
    } else {
      throw new CustomException(ResponseCodeEnum.BAD_REQUEST);
    }
    return JsonUtil.obj2Json(ResponseVo.success(ResponseCodeEnum.OK_PATCHED));
  }

  @Override
  public String queryById(int id) {
    Task task = taskMapper.getDetailById(id);
    return JsonUtil.obj2Json(ResponseVo.success(task));
  }

  @Override
  public String listPage(int pageCurrent, int pageSize) {
    Page<Task> page = new Page<>(pageCurrent, pageSize);
    List<Task> listData = taskMapper.listPage(page).getRecords();
    return ResponseUtil.writePageData(page, listData, pageSize);
  }

}




